<%
	'## 공통코드 콤보박스 만드는 함수
	Function mkComCombo(comCd)	
		SQL = "SELECT itemNm, itemCd FROM tblComCode WHERE flag='Y' AND groupCd='" & comCd & "' ORDER BY itemSeq" 
	
		Set Rs=Server.CreateObject("ADODB.RecordSet")
		Rs.open SQL, DbCon, 1
		
		options = ""
		If Rs.eof Or Rs.bof Then 
		Else 
			Do Until Rs.eof Or Rs.bof			
			options = options & "<option value=""" & Rs("itemCd") & """>" & Rs("itemNm") & "</option>"
			Rs.movenext
			Loop 
		End If
		mkComCombo = options
	End Function
	
	'## 쿼리로 콤보박스 만드는 함수
	Function mkSQLCombo(sql)	
	
		Set Rs=Server.CreateObject("ADODB.RecordSet")
		Rs.open SQL, DbCon, 1
		
		options = ""
		If Rs.eof Or Rs.bof Then 
		Else 
			Do Until Rs.eof Or Rs.bof			
			options = options & "<option value=""" & Rs(0) & """>" & Rs(1) & "</option>"
			Rs.movenext
			Loop 
		End If
		mkSQLCombo = options
	End Function
	
	'## 해당 날짜의 마지막 날짜 구해주는 함수
	Function getLastDay(ymd)
		stdYr = Left(ymd,4)
		stdMnth = Mid(ymd,6,2)
		nextMnth = dateadd("m",1, stdYr & "-" & stdMnth & "-01")
		nextMnthLastDay = dateadd("d",-1, nextMnth)
		getLastDay = Right(nextMnthLastDay,2)	
	End Function 
	
	'## 소수점 3자리 만들어 주는 함수
	Function mkRateFormat(rate)
		divRate = Split(rate,".")
		underSpot = ""
		If ubound(divRate) = 0 Then 
			mkRateFormat = divRate(0) & ".000"
		Else	
			If Len(divRate(1)) = 0 Then 
				underSpot = "000"
			ElseIf Len(divRate(1)) = 1 Then 
				underSpot = "00"
			ElseIf Len(divRate(1)) = 2 Then 
				underSpot = "0"
			End If 
			mkRateFormat = divRate(0) & "." & divRate(1) & underSpot
		End If 
	End Function 
	
	'## 한개의 필드의 값을 구하는 함수
	Function aFieldValue(Q)		
		Set Rsf=Server.CreateObject("ADODB.RecordSet")
		Rsf.open Q, dbCon
		if Rsf.eof or Rsf.bof then
			aFieldValue = ""
		Else
			aFieldValue = Rsf(0)				
		End If
	End Function

	'## 현재 시간 구하는 함수
	Function curTime() 
		curDate = Replace(Date(),"-","")
		strHr = hour(now):    if len(strHr)   = 1 Then strHr   = "0"  & strHr
		strMn = minute(now):  if len(strMn) = 1 Then strMn = "0"  & strMn
		strSc = second(now):  if len(strSc) = 1 Then strSc = "0"  & strSc

		curTime = curDate & strHr & strMn & strSc
	End Function 
	
	'## 파일에 쓰기 함수
	Sub WriteToFile (strText, strFile, encode)''utf-8에서 사용시
		Dim Stream
		Set Stream = CreateObject("ADODB.Stream")
		With Stream
		.Open
	'	.CharSet = "utf-8"
		.CharSet = encode
		.WriteText strText
		.SaveToFile strFile, 2
		End With
		Set Stream = Nothing
	End Sub
	
	'## XML format Response
	Sub rsWithXML(Str)
		'Response.write "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbcrlf
		Response.write "<Response>" & vbcrlf
		Response.write "	<MSG>" & Str & "</MSG>" & vbcrlf
		Response.write "</Response>" & vbcrlf
	End Sub 

	'## XML format Response
	Sub rsWithXML2(Str,Val)
		'Response.write "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbcrlf
		Response.write "<Response>" & vbcrlf
		Response.write "	<MSG>" & Str & "</MSG>" & vbcrlf
		Response.write "	<VAL>" & Val & "</VAL>" & vbcrlf
		Response.write "</Response>" & vbcrlf
	End Sub 
	
	'## SQL Query 예약어 구분
	Function SQLReserved(query)		
		SQLReserved = Replace(query, "SELECT", "<b>SELECT</b>")
		SQLReserved = Replace(SQLReserved, "FROM", "<b>FROM</b>")
		SQLReserved = Replace(SQLReserved, "WHERE", "<b>WHERE</b>")
		SQLReserved = Replace(SQLReserved, "ORDER BY", "<b>ORDER BY</b>")
		SQLReserved = Replace(SQLReserved, "GROUP BY", "<b>GROUP BY</b>")
		SQLReserved = Replace(SQLReserved, "UPDATE", "<b>UPDATE</b>")
		SQLReserved = Replace(SQLReserved, "DELETE", "<b>DELETE</b>")
		SQLReserved = Replace(SQLReserved, "WHEN ", "<b>WHEN </b>")
		SQLReserved = Replace(SQLReserved, "THEN ", "<b>THEN </b>")
		SQLReserved = Replace(SQLReserved, "CASE ", "<b>CASE </b>")
		SQLReserved = Replace(SQLReserved, "ELSE ", "<b>ELSE </b>")
		SQLReserved = Replace(SQLReserved, "IF ", "<b>IF </b>")
		SQLReserved = Replace(SQLReserved, "INSERT ", "<b>INSERT </b>")
		SQLReserved = Replace(SQLReserved, "VALUES", "<b>VALUES</b>")
		SQLReserved = Replace(SQLReserved, "MERGE ", "<b>MERGE </b>")
		SQLReserved = Replace(SQLReserved, "SET ", "<b>SET </b>")
		SQLReserved = Replace(SQLReserved, "ON ", "<b>ON </b>")
	End Function 
	
	'## Debug Mode
	Sub showDebug(title, query)
		sqlStr = Replace(query, vbCrlf, "<br>")
		sqlStr = Replace(sqlStr, "	", "&nbsp; &nbsp; &nbsp;")
		sqlStr = SQLReserved(sqlStr)
		Response.write "<span style=""font-size:12px;font-family:명조"">" & title & "</span><br>" & vbcrlf
		Response.write "<span style=""font-size:9pt;font-family:Verdana"">" & sqlStr & "</span><br><br>" & vbcrlf
		Response.write "<script>setTimeout(""self.close()"",20000);</script>"
	End Sub
	
	'## Make XML node
	Sub mkXMLNode(node, str, tap)
		tapStr = ""
		for k=1 to tap
			tapStr = tapStr & "	"
		next
		Response.write tapStr & "<" & node & "><![CDATA[" & str & "]]></" & node & ">" & vbcrlf
	End Sub

	'## Make XML node with Attribute
	Sub mkXMLNodeAttr(node, str, tap, attr)
		tapStr = ""
		for k=1 to tap
			tapStr = tapStr & "	"
		next
		Response.write tapStr & "<" & node & " " & attr & "><![CDATA[" & str & "]]></" & node & ">" & vbcrlf
	End Sub
	
	'## 월별 불량율 계산하는 함수
	Function getBadRate(mnth)
		bfMonth = Left(mnth,7)
		bfMonthRate10 = aFieldValue("SELECT (CAST(SUM(isnull(goodQty,0)) as varchar(10))+'/'+CAST(SUM(isnull(badQty,0)) as varchar(10))) FROM tblOrderResult A INNER JOIN tblOrder B ON A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo AND A.lotYear=B.lotYear WHERE A.type='10' AND A.flag='Y' AND B.flag<>'N' AND B.type='L' AND A.workDt Like '" & bfMonth & "%'") 
		bfMonthRate20 = aFieldValue("SELECT isnull(SUM(badQty),0) FROM tblOrderResult A INNER JOIN tblOrder B ON A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo AND A.lotYear=B.lotYear WHERE A.type='20' AND A.flag='Y' AND B.flag<>'N' AND B.type='L' AND A.workDt Like '" & bfMonth & "%'")
		If isnull(bfMonthRate10) = False Then 
			divBfMonthRate = Split(bfMonthRate10,"/")
			If (CDbl(divBfMonthRate(0))+CDbl(divBfMonthRate(1))) > 0 Then 
				bfMonthBadRate = (CDbl(divBfMonthRate(1))+CDbl(bfMonthRate20))/(CDbl(divBfMonthRate(0))+CDbl(divBfMonthRate(1))) * 100
				bfMnthBadRate = mkRateFormat(Round(bfMonthBadRate,3))
				getBadRate = Right(bfMonth,2) & "월 : " & bfMnthBadRate & "%"
			Else
				getBadRate = Right(bfMonth,2) & "월 : 0%"
			End If
		Else 
			getBadRate = Right(bfMonth,2) & "월 : 0%"
		End If 
	End Function 
	
	'## 구글 메일러를 통한 메일 보내기
	
	Function GoogleSendMailer(strTo, strFrom, strSubject, strBody)
		On Error Resume Next
		Set iMsg = CreateObject("CDO.Message")
		Set iConf = CreateObject("CDO.Configuration")
		Set Flds = iConf.Fields
		schema = "http://schemas.microsoft.com/cdo/configuration/"
		Flds.Item(schema & "sendusing") = 2
		Flds.Item(schema & "smtpaccountname") = strFrom '"Jade <hooshim@gmail.com>"
		Flds.Item(schema & "sendmailaddress") = strFrom '"Jade <hooshim@gmail.com>"
		Flds.Item(schema & "smtpuserreplymailaddress") = strFrom '"Jade <hooshim@gmail.com>"
		Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
		Flds.Item(schema & "smtpserverport") = 465
		Flds.Item(schema & "smtpauthenticate") = 1
		Flds.Item(schema & "sendusername") = "hooshim@gmail.com"
		Flds.Item(schema & "sendpassword") = "jj101010"		
		Flds.Item(schema & "smtpusessl") = 1
		Flds.Update
		Set Flds = Nothing
		Set iMsg = Server.CreateObject("CDO.Message")
		With iMsg
			.Configuration = iConf
			.To = strTo
			.From = strFrom
			.Subject = strSubject
			.HTMLBody = strBody
			SendEmailGmail = .Send
		End With
		Set iMsg = Nothing
		Set iConf = Nothing
		Set Flds = Nothing
		If Err.number <> 0 Then
			GoogleSendMail = Err.Description
		Else
			GoogleSendMail = 0
		End If 
	End Function 
	
%>	